﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：NodesTransaction.cs
           // 文件功能描述：对TreeNode的处理。
           //
           // 
           // 创建标识：杨波20080907
           //
           // 修改标识： 杨波20090303
           // 修改描述：修正连续添加两个相同EDT引发的Bug
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.Data;
using System.Xml;

#endregion

namespace RuleCode.ObjectTree
{
    internal class NodesTransaction
    {
        private static NodesTransaction instance;

        /// <summary>
        /// 获得单态实例。
        /// </summary>
        public static NodesTransaction Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new NodesTransaction();
                }
                return instance;
            }
        }
        public void MoveToBottom(SysTreeNode node)
        {
            XmlNode xmlNode = node.SysXmlNode;
            if (!(node.Nodes.Count > 0))
            {
                SysTreeNode bottomTreeNode = (SysTreeNode)node.Parent.LastNode;
                XmlNode bottomXmlNode = bottomTreeNode.SysXmlNode;
                xmlNode.ParentNode.InsertAfter(xmlNode, bottomXmlNode);
                SysTreeNode temp = (SysTreeNode)node.Clone();


                //Important:此处只复制了有用的信息。

                node.Text = bottomTreeNode.Text;
                node.Property = bottomTreeNode.Property;
                node.SysXmlNode = bottomTreeNode.SysXmlNode;


                bottomTreeNode.Text = temp.Text;
                bottomTreeNode.Property = temp.Property;
                bottomTreeNode.SysXmlNode = temp.SysXmlNode;

                Util.SelectedNode = bottomTreeNode;
            }
        }
        public void MoveToTop(SysTreeNode node)
        {
            XmlNode xmlNode = node.SysXmlNode;
            if (!(node.Nodes.Count > 0))
            {
                SysTreeNode topTreeNode = (SysTreeNode)node.Parent.FirstNode;
                XmlNode topXmlNode = topTreeNode.SysXmlNode;
                xmlNode.ParentNode.InsertAfter(xmlNode, topXmlNode);
                SysTreeNode temp = (SysTreeNode)node.Clone();


                //Important:此处只复制了有用的信息。

                node.Text = topTreeNode.Text;
                node.Property = topTreeNode.Property;
                node.SysXmlNode = topTreeNode.SysXmlNode;


                topTreeNode.Text = temp.Text;
                topTreeNode.Property = temp.Property;
                topTreeNode.SysXmlNode = temp.SysXmlNode;

                Util.SelectedNode = topTreeNode;
            }
        }
    
        public void UpNode(SysTreeNode node)
        {
            XmlNode xmlNode = node.SysXmlNode;
            //Bug：定义可以向上移动的结点类型,暂时所有节点都可移动
            //if (xmlNode.Attributes["Type"].Value.Equals(OT.TableField))
            //{
            //第一个结点不能向上移动

            //modified by yang bo @ 20100113有子结点的不能移动
            if (!node.Parent.FirstNode.Equals(node)&&!(node.Nodes.Count>0))
            {
                SysTreeNode preTreeNode = (SysTreeNode)node.PrevNode;
                //int preIndex = preTreeNode.Index;
                XmlNode preXmlNode = preTreeNode.SysXmlNode;
                //新加结点不能移动
                if (xmlNode.ParentNode == null)
                {
                    //if (node.Parent != null)
                    //{
                    //    parent = ((SysTreeNode)node.Parent).SysXmlNode;
                    //    parent.InsertAfter(xmlNode, nextXmlNode);
                    //}
                    //else
                    //{

                    //}
                }
                else
                {
                    xmlNode.ParentNode.InsertAfter(xmlNode, preXmlNode);
                }
                //TreeNode parent = node.Parent;
                /*//不好使
                XmlNode tempXmlNode = preXmlNode;
                xmlNode.ParentNode.ReplaceChild(preXmlNode, xmlNode);
                xmlNode.ParentNode.ReplaceChild(xmlNode, tempXmlNode);
                */

                SysTreeNode temp = (SysTreeNode)node.Clone();


                //Important:此处只复制了有用的信息。

                node.Text = preTreeNode.Text;
                node.Property = preTreeNode.Property;
                node.SysXmlNode = preTreeNode.SysXmlNode;


                preTreeNode.Text = temp.Text;
                preTreeNode.Property = temp.Property;
                preTreeNode.SysXmlNode = temp.SysXmlNode;

                Util.SelectedNode = preTreeNode;
                //}
            }
        }

        public void DownNode(SysTreeNode node)
        {
            XmlNode xmlNode = node.SysXmlNode;
            XmlNode parent;

            //if (xmlNode.Attributes["Type"].Value.Equals(OT.TableField))
            //{
            if (!node.Parent.LastNode.Equals(node)&&!(node.Nodes.Count>0))
            {
                SysTreeNode nextTreeNode = (SysTreeNode)node.NextNode;
                //int nextIndex = node.Index;
                XmlNode nextXmlNode = nextTreeNode.SysXmlNode;
                //TreeNode parent = node.Parent;
                //新加结点XML没有父结点，先从TreeNode找，也没有TreeNode，就要退出再移动
                //新加结点不能移动

                if (xmlNode.ParentNode == null)
                {
                    //if (node.Parent != null)
                    //{
                    //    parent = ((SysTreeNode)node.Parent).SysXmlNode;
                    //    parent.InsertAfter(xmlNode, nextXmlNode);
                    //}
                    //else
                    //{

                    //}
                }
                else
                {
                    xmlNode.ParentNode.InsertAfter(xmlNode, nextXmlNode);
                }
                /*
                nextTreeNode.Remove();
                node.Remove();
                parent.Nodes.Insert(nextIndex, nextTreeNode);
                parent.Nodes.Insert(nextIndex + 1, node);
                 */
                SysTreeNode temp = (SysTreeNode)node.Clone();

                node.Text = nextTreeNode.Text;
                node.Property = nextTreeNode.Property;
                node.SysXmlNode = nextTreeNode.SysXmlNode;

                nextTreeNode.Text = temp.Text;
                nextTreeNode.Property = temp.Property;
                nextTreeNode.SysXmlNode = temp.SysXmlNode;

                Util.SelectedNode = nextTreeNode;
                //Util.ActiveTreeView.SelectedNode = node;
                //}
            }
        }

        public void SortXmlNode(XmlNode node)
        {
            int count = node.ChildNodes.Count;
            bool done = false;
            int j = 1;

            while ((j < count) && (!done))
            {
                done = true;

                for (int i = 0; i < count - j; i++)
                {
                    if (node.ChildNodes[i].Name.CompareTo(node.ChildNodes[i + 1].Name) > 0)
                    {
                        done = false;
                        node.InsertBefore(node.ChildNodes[i + 1], node.ChildNodes[i]);
                        /*
                        XmlNode temp = node.ChildNodes[i];
                        node.ReplaceChild(node.ChildNodes[i], node.ChildNodes[i + 1]);
                        node.ReplaceChild(node.ChildNodes[i + 1], temp);
                        */
                    }
                }
                j++;
            }
        }

        public void addMenuItemInMenu()
        {
            string name = Util.addNode(OT.XPathSeperater + OT.MenuItemInMenu, OT.Item, typeof(MenuItemInMenuProperty), "", false);
            ((MenuItemInMenuProperty)Util.SelectedNode.Property).Name = name;
        }

        //Modified by DingYuliang 2009年2月5日
        //Added Code
        public void addMenuItemInMenu(SysTreeNode draged)
        {
            Util.addNode(OT.XPathSeperater + OT.MenuItemInMenu, OT.Item, typeof(MenuItemInMenuProperty), "", false);
            string name = draged.Text;

            ((MenuItemInMenuProperty)Util.SelectedNode.Property).Name = name;
            ((MenuItemInMenuProperty)Util.SelectedNode.Property).Label = ((MenuItemProperty)draged.Property).Label;
            ((MenuItemInMenuProperty)Util.SelectedNode.Property).MenuItemName = ((MenuItemProperty)draged.Property).Name;
            Util.SelectedNode.Text = name;
            Util.SelectedNode.SysXmlNode = Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, name);

        }
        public void addWebFormInMenu(SysTreeNode draged)
        {
            Util.addNode(OT.XPathSeperater + OT.MenuItemInMenu, OT.Item, typeof(MenuItemInMenuProperty), "", false);
            string name = draged.Text;

            ((MenuItemInMenuProperty)Util.SelectedNode.Property).Name = name;
            ((MenuItemInMenuProperty)Util.SelectedNode.Property).Label = ((HelpTextProperty)draged.Property).Label;
            ((MenuItemInMenuProperty)Util.SelectedNode.Property).MenuItemName = ((HelpTextProperty)draged.Property).Name;
            Util.SelectedNode.Text = name;
            Util.SelectedNode.SysXmlNode = Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, name);
 
        }
        //end modified by DingYuliang 2009年2月5日



        public void addQueryDataSource(SysTreeNode draged)
        {
            Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.QueryDataSources, OT.QueryDataSource,
                         typeof(QueryDataSourceProperty), "", false);
            string name = draged.Text;
            ((QueryDataSourceProperty)Util.SelectedNode.Property).Table = name;
            ((QueryDataSourceProperty)Util.SelectedNode.Property).Name = name;
            Util.SelectedNode.Text = name;
            Util.SelectedNode.SysXmlNode = Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, name);

            Util.SelectedNode.Expand();
            //SysTreeNode FieldsNode = (SysTreeNode)Util.SelectedNode.Nodes.Find("Fields", true)[0];
            SysTreeNode FieldsNode = Util.SelectedNode.FindNode(OT.Fields);
            Util.SelectedNode = FieldsNode;
            draged.Expand();
            //SysTreeNode tableFields = (SysTreeNode)draged.Nodes.Find("Fields", true)[0];
            SysTreeNode tableFields = draged.FindNode(OT.Fields);
            tableFields.Expand();

            foreach (SysTreeNode sysTreeNode in tableFields.Nodes)
            {
                Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.QueryDataSourceFields, OT.Field,
                             typeof(QueryDataSourceFieldProperty), "", false);
                ((QueryDataSourceFieldProperty)Util.SelectedNode.Property).Table = name;
                ((QueryDataSourceFieldProperty)Util.SelectedNode.Property).Field = sysTreeNode.Text;
                Util.SelectedNode.Text = sysTreeNode.Text;
                Util.SelectedNode.SysXmlNode = Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, sysTreeNode.Text);
                Util.SelectedNode = FieldsNode;
            }
        }



        public void addMenuItem(SysTreeNode draged)
        {
            Util.addNode(OT.XPathSeperater + OT.MenuItemGroup, OT.Item, typeof(MenuItemProperty), "", false);
            string name = draged.Text;
            ((MenuItemProperty)Util.SelectedNode.Property).Name = name;
            Util.SelectedNode.Text = name;
            Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, name);
        }

        public bool isCanBeDropped(SysTreeNode dragged, SysTreeNode target)
        {
            bool ret = false;
            string draggedNodeType = dragged.SysXmlNode.Attributes.GetNamedItem("Type").Value;
            string targetNodeType = target.SysXmlNode.Attributes.GetNamedItem("Type").Value;
            switch (draggedNodeType)
            {
                case OT.ExtendDataType:
                    {
                        if (targetNodeType.Equals(OT.TableFields))
                            ret = true;
                        break;
                    }
                //case OT.MenuItem:
                //    {
                //        if (targetNodeType.Equals(OT.Menu))
                //        {
                //            ret = true;
                //        }
                //        break;
                //    }
                case OT.Form:
                    {
                        if (targetNodeType.Equals(OT.MenuItemGroup))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.Table:
                    {
                        if (targetNodeType.Equals(OT.QueryDataSources))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormMasterSlave:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormSingle:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormParaByUser:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormParameter:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormRelation:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormTree:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                case OT.WebFormBlank:
                    {
                        if (targetNodeType.Equals(OT.Menu))
                        {
                            ret = true;
                        }
                        break;
                    }
                //允许字段拖到字段组
                case OT.TableField:
                    {
                        if (targetNodeType.Equals(OT.FieldGroup))
                        {
                            ret = true;
                        }
                        break;
                    }
                default:
                    break;
            }
            return ret;
        }

        //根据拖动结点进行Drop操作。
        public void doDrop(SysTreeNode draged, SysTreeNode target)
        {
            target.Expand();
            switch (target.SysXmlNode.Attributes.GetNamedItem("Type").Value)
            {
                case OT.TableFields:
                    {
                        addTableField(target, draged);
                        //XmlNode tableFieldsTemplateNode = TreeViewOpr.nodesTemplate.SelectSingleNode("//TableFields");
                        ////初始化拖动结点属性。
                        ////Util.SetPropertyContent(draged);                    
                        //XmlNode tableFieldNode = tableFieldsTemplateNode.CloneNode(true).FirstChild;

                        //tableFieldNode = Util.SysXmlDocument.ImportNode(tableFieldNode, true);

                        //try
                        //{
                        //    //target.SysXmlNode.InsertAfter(tableFieldNode, target.SysXmlNode.FirstChild);
                        //    target.SysXmlNode.AppendChild(tableFieldNode);
                        //}
                        //catch (Exception e)
                        //{

                        //    System.Console.WriteLine(e.ToString());
                        //}


                        //string ID = Util.getMaxTypeID(tableFieldNode);

                        //string name = draged.Text;
                        //TableFieldProperty property;
                        //Util.SetPropertyInSilent = true;
                        //Util.SetPropertyContent(draged);
                        //Util.SetPropertyInSilent = false;

                        //switch (((EDTProperty)draged.Property).EDTType)
                        //{
                        //    case EDTTypes.EDTTypeTime:
                        //        {
                        //            property = new TableFieldTimeProperty(ID);
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeString:
                        //        {
                        //            property = new TableFieldStringProperty(ID);
                        //            ((TableFieldStringProperty)property).StringSize =
                        //                ((EDTStringProperty)draged.Property).StringSize;
                        //            ((TableFieldStringProperty)property).Adjustment =
                        //                ((EDTStringProperty)draged.Property).Adjustment;
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeReal:
                        //        {
                        //            property = new TableFieldRealProperty(ID);
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeInt:
                        //        {
                        //            property = new TableFieldIntProperty(ID);
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeEnum:
                        //        {
                        //            property = new TableFieldEnumProperty(ID);
                        //            (property as TableFieldEnumProperty).EnumType =
                        //                (draged.Property as EDTEnumProperty).EnumType;
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeDate:
                        //        {
                        //            property = new TableFieldDateProperty(ID);
                        //            break;
                        //        }
                        //    case EDTTypes.EDTtypeContainer:
                        //        {
                        //            property = new TableFieldContainerProperty(ID);
                        //            break;
                        //        }
                        //    case EDTTypes.EDTTypeInt64:
                        //        {
                        //            property = new TableFieldInt64Property(ID);
                        //            break;
                        //        }

                        //    default:
                        //        {
                        //            property = new TableFieldProperty(ID);
                        //            break;
                        //        }
                        //}
                        ////TableFieldProperty property = new TableFieldProperty(ID);
                        //property.ExtendedDataType = name;

                        //tableFieldNode = Util.changeXmlNodeName(tableFieldNode, name);
                        //tableFieldNode.Value = name;
                        //tableFieldNode.InnerXml = tableFieldNode.InnerXml.Replace(tableFieldNode.Name, name);
                        //property.Label = (draged.Property as EDTProperty).Label;
                        //property.LabelText = (draged.Property as EDTProperty).LabelText;
                        //tableFieldNode = Util.changeXmlNodeName(tableFieldNode, name);
                        ////tableFieldNode.Value = name;
                        ////tableFieldNode.InnerXml = tableFieldNode.InnerXml.Replace(tableFieldNode.Name, name);

                        //string newName = tableFieldNode.Name;
                        ////检查是否与已有字段重名
                        //foreach (SysTreeNode node in target.Nodes)
                        //{
                        //    if (node.Name == tableFieldNode.Name)
                        //    {
                        //        // 重名，则添加ID为后缀
                        //        newName += ID;
                        //    }
                        //}
                        //SysTreeNode newTableField = new SysTreeNode(newName);

                        //tableFieldNode = Util.changeXmlNodeName(tableFieldNode, newName);
                        //newTableField.Name = newName;
                        //property.Name = name;
                        //newTableField.Property = property;

                        //newTableField.SysXmlNode = tableFieldNode;
                        //target.Nodes.Insert(0, newTableField);
                        //target.Expand();
                        //Util.ActiveTreeView.SelectedNode = newTableField;
                        //Util.SetPropertyContent(property);

                        ////在表中添加该字段。
                        //Util.SetPropertyContent(draged);
                        //try
                        //{
                        //    SqlHelper helper = SqlHelper.Instance;
                        //    helper.ExecuteNonQuery(CommandType.Text,
                        //                           "alter table [" + target.SysXmlNode.ParentNode.Name + "] add [" +
                        //                           tableFieldNode.Name + "] " +
                        //                           Util.getTableFieldFormat(((EDTProperty)draged.Property).EDTType,
                        //                                                    (EDTProperty)draged.Property));
                        //    //抛出Exception就不序列化属性
                        //    property.SerializeToXML();
                        //}
                        //catch (Exception e)
                        //{
                        //    target.SysXmlNode.RemoveChild(tableFieldNode);
                        //    target.Nodes.Remove(newTableField);
                        //    throw;
                        //}


                        //TreeViewOpr.saveToXML();


                        break;
                    }
                case OT.Menu:
                    {
                        //Modified by DingYuliang 2009年2月5日
                        //addMenuItemInMenu();      //Origin Code

                        //addMenuItemInMenu(draged);  //Modified Code
                        //end Modified by DingYuliang 2009年2月5日
                        addWebFormInMenu(draged);
                        break;
                    }
                case OT.MenuItemGroup:
                    {
                        addMenuItem(draged);
                        break;
                    }
                case OT.QueryDataSources:
                    {
                        addQueryDataSource(draged);
                        break;
                    }
                case OT.FieldGroup:
                    {
                        addTableFieldGroupField(draged);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
        /// <summary>
        /// 在字段组中新建拖动的字段
        /// </summary>
        /// <param name="draged"></param>
        private void addTableFieldGroupField(SysTreeNode draged)
        {
            //throw new NotImplementedException();
            Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.FieldGroup, OT.GroupField,
                                      typeof(GroupFieldProperty), string.Empty, false,NewNodePosition.Bottom);
            string name = draged.Text;

            ((GroupFieldProperty)Util.SelectedNode.Property).DataField = name;
            Util.SelectedNode.Text = name;
            Util.changeXmlNodeName(Util.SelectedNode.SysXmlNode, name);
           // MoveToBottom(Util.SelectedNode);

        }
        /// <summary>
        /// 为了修改新加表不能拖动添加字段的Bug，新加此方法，达到要求。
        /// </summary>
        /// <param name="target"></param>
        /// <param name="draged"></param>
        private void addTableField(SysTreeNode target, SysTreeNode draged)
        {
            Util.SetPropertyInSilent = true;
            Util.SetPropertyContent(draged);
            Util.SetPropertyInSilent = false;
            //获取类型
            Type fieldProperty = null;
            switch (((EDTProperty)draged.Property).EDTType)
            {
                case EDTTypes.EDTTypeTime:
                    {
                        fieldProperty = typeof(TableFieldTimeProperty);
                        break;
                    }
                case EDTTypes.EDTTypeString:
                    {
                        fieldProperty = typeof(TableFieldStringProperty);
                        break;
                    }
                case EDTTypes.EDTTypeReal:
                    {
                        fieldProperty = typeof(TableFieldRealProperty);
                        break;
                    }
                case EDTTypes.EDTTypeInt64:
                    {
                        fieldProperty = typeof(TableFieldInt64Property);
                        break;
                    }
                case EDTTypes.EDTTypeInt:
                    {
                        fieldProperty = typeof(TableFieldIntProperty);
                        break;
                    }
                case EDTTypes.EDTTypeEnum:
                    {
                        fieldProperty = typeof(TableFieldEnumProperty);
                        break;

                    }
                case EDTTypes.EDTtypeContainer:
                    {
                        fieldProperty = typeof(TableFieldContainerProperty);
                        break;
                    }
                case EDTTypes.EDTTypeDate:
                    {
                        fieldProperty = typeof(TableFieldDateProperty);
                        break;
                    }

            }
            //检查是否与已有字段重名
            string name = draged.Text;
            //检查是否与已有字段重名
            foreach (SysTreeNode node in target.Nodes)
            {
                if (node.Name == name)
                {
                    // 重名，则添加ID为后缀
                    name = name + (Convert.ToString(new Random().Next()));
                }
            }
            string fieldname = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.TableFields, OT.TableField,
                                            fieldProperty, name, false);
            TableFieldProperty property = Util.SelectedNode.Property as TableFieldProperty;
            switch (((EDTProperty)draged.Property).EDTType)
            {
                case EDTTypes.EDTTypeString:
                    {

                        ((TableFieldStringProperty)property).StringSize =
                            ((EDTStringProperty)draged.Property).StringSize;
                        ((TableFieldStringProperty)property).Adjustment =
                            ((EDTStringProperty)draged.Property).Adjustment;
                        break;
                    }
                case EDTTypes.EDTTypeEnum:
                    {
                        (property as TableFieldEnumProperty).EnumType =
                            (draged.Property as EDTEnumProperty).EnumType;
                        break;
                    }

            }
            property.ExtendedDataType = draged.Text;

            property.Label = (draged.Property as EDTProperty).Label;
            property.LabelText = (draged.Property as EDTProperty).LabelText;
            property.HelpLabel = (draged.Property as EDTProperty).HelpLabel;
            property.HelpText = (draged.Property as EDTProperty).HelpText;

            try
            {

                SqlHelper helper = SqlHelper.Instance;
                if (target.Parent != null)
                {
                    helper.ExecuteNonQuery(CommandType.Text,
                                           "alter table [" + target.Parent.Text + "] add [" +
                                           name + "] " +
                                           Util.getTableFieldFormat(((EDTProperty)draged.Property).EDTType,
                                                                    (EDTProperty)draged.Property));
                }
                else
                {
                    foreach (SysTreeView sysTreeView in TreeViewOpr.LivelySysTreeViewList)
                    {
                        sysTreeView.Enabled = false;
                    }
                    //MessageBox.Show("添加失败。打开新OT添加对象。");
                    throw new Exception();
                }
                //抛出Exception就不序列化属性
                property.SerializeToXML();
            }
            catch (Exception e)
            {
                target.SysXmlNode.RemoveChild(Util.SelectedNode.SysXmlNode);
                target.Nodes.Remove(Util.SelectedNode);
                throw;
            }


            




            //XmlNode tableFieldsTemplateNode = TreeViewOpr.nodesTemplate.SelectSingleNode("//TableFields");
            ////初始化拖动结点属性。
            ////Util.SetPropertyContent(draged);                    
            //XmlNode tableFieldNode = tableFieldsTemplateNode.CloneNode(true).FirstChild;

            //tableFieldNode = Util.SysXmlDocument.ImportNode(tableFieldNode, true);


            ////target.SysXmlNode.InsertAfter(tableFieldNode, target.SysXmlNode.FirstChild);
            //target.SysXmlNode.AppendChild(tableFieldNode);



            //string ID = Util.getMaxTypeID(tableFieldNode);

            //string name = draged.Text;
            //TableFieldProperty property;
            //Util.SetPropertyInSilent = true;
            //Util.SetPropertyContent(draged);
            //Util.SetPropertyInSilent = false;

            //switch (((EDTProperty)draged.Property).EDTType)
            //{
            //    case EDTTypes.EDTTypeTime:
            //        {
            //            property = new TableFieldTimeProperty(ID);
            //            break;
            //        }
            //    case EDTTypes.EDTTypeString:
            //        {
            //            property = new TableFieldStringProperty(ID);
            //            ((TableFieldStringProperty)property).StringSize =
            //                ((EDTStringProperty)draged.Property).StringSize;
            //            ((TableFieldStringProperty)property).Adjustment =
            //                ((EDTStringProperty)draged.Property).Adjustment;
            //            break;
            //        }
            //    case EDTTypes.EDTTypeReal:
            //        {
            //            property = new TableFieldRealProperty(ID);
            //            break;
            //        }
            //    case EDTTypes.EDTTypeInt:
            //        {
            //            property = new TableFieldIntProperty(ID);
            //            break;
            //        }
            //    case EDTTypes.EDTTypeEnum:
            //        {
            //            property = new TableFieldEnumProperty(ID);
            //            (property as TableFieldEnumProperty).EnumType =
            //                (draged.Property as EDTEnumProperty).EnumType;
            //            break;
            //        }
            //    case EDTTypes.EDTTypeDate:
            //        {
            //            property = new TableFieldDateProperty(ID);
            //            break;
            //        }
            //    case EDTTypes.EDTtypeContainer:
            //        {
            //            property = new TableFieldContainerProperty(ID);
            //            break;
            //        }
            //    case EDTTypes.EDTTypeInt64:
            //        {
            //            property = new TableFieldInt64Property(ID);
            //            break;
            //        }

            //    default:
            //        {
            //            property = new TableFieldProperty(ID);
            //            break;
            //        }
            //}
            ////TableFieldProperty property = new TableFieldProperty(ID);
            //property.ExtendedDataType = name;

            //property.Label = (draged.Property as EDTProperty).Label;
            //property.LabelText = (draged.Property as EDTProperty).LabelText;
            //tableFieldNode = Util.changeXmlNodeName(tableFieldNode, name);
            ////tableFieldNode.Value = name;
            ////tableFieldNode.InnerXml = tableFieldNode.InnerXml.Replace(tableFieldNode.Name, name);

            //string newName = tableFieldNode.Name;
            ////检查是否与已有字段重名
            //foreach (SysTreeNode node in target.Nodes)
            //{
            //    if (node.Name == tableFieldNode.Name)
            //    {
            //        // 重名，则添加ID为后缀
            //        newName += ID;
            //    }
            //}
            //SysTreeNode newTableField = new SysTreeNode(newName);

            //tableFieldNode = Util.changeXmlNodeName(tableFieldNode, newName);
            //newTableField.Name = newName;
            //property.Name = name;
            //newTableField.Property = property;

            //newTableField.SysXmlNode = tableFieldNode;
            //target.Nodes.Insert(0, newTableField);
            //target.Expand();
            //Util.ActiveTreeView.SelectedNode = newTableField;
            //Util.SetPropertyContent(property);

            ////在表中添加该字段。
            //Util.SetPropertyContent(draged);
            //try
            //{
            //    SqlHelper helper = SqlHelper.Instance;
            //    helper.ExecuteNonQuery(CommandType.Text,
            //                           "alter table [" + target.SysXmlNode.ParentNode.Name + "] add [" +
            //                           tableFieldNode.Name + "] " +
            //                           Util.getTableFieldFormat(((EDTProperty)draged.Property).EDTType,
            //                                                    (EDTProperty)draged.Property));
            //    //抛出Exception就不序列化属性
            //    property.SerializeToXML();
            //}
            //catch (Exception e)
            //{
            //    target.SysXmlNode.RemoveChild(tableFieldNode);
            //    target.Nodes.Remove(newTableField);
            //    throw;
            //}


            //TreeViewOpr.saveToXML();
        }
    }
}